<?php
include("utils.php");

if($_SESSION['siteuserId']=="")
{
	header("location:start.php");
}
if($_POST['mode']=="change_status")
{
	change_status();
}
else
{
	main_content("main();");
}

function change_status()
{
   $sql = "UPDATE ".ADSPSCEMASTER." SET status = if(status = 'N','Y','N') WHERE adspaceId = '".$_POST['Id']."' ";
	mysql_query($sql);
	$GLOBALS['err_msg'] = AD_STATUS_CHANGED;
	main_content("main();");

}

function main()
{

if($_SESSION['languageId']!="")
{
	$languageId=$_SESSION['languageId'];
}
else
{
	$languageId='1';
}

if($_POST['space_type']!="")
{
	 $sql_details="select * from ".SITEMASTER." as SM ,".ADSPSCEMASTER." AM,".ADSPACEDETAILS." SD,".CATEGORIES." CA where SM.adsiteId=AM.adsiteId and AM.langId='".$languageId."' and AM.UserId='".$_SESSION['siteuserId']."' and SD.adspaceId=AM.adspaceId and SM.adcategoryId=CA.adcategoryId and SD.adspaceType='".$_POST['space_type']."' order by AM.readyDate desc ";
}

elseif($_POST['site_name']!="")
{
	$sql_details="select * from ".SITEMASTER." as SM ,".ADSPSCEMASTER." AM,".ADSPACEDETAILS." SD,".CATEGORIES." CA where SM.adsiteId=AM.adsiteId and AM.langId='".$languageId."' and AM.UserId='".$_SESSION['siteuserId']."' and AM.adspaceId=SD.adspaceId and SM.adcategoryId=CA.adcategoryId and SM.adSiteName='".$_POST['site_name']."' order by AM.readyDate desc ";
}

else
{
// $sql_details="select * from ".SITEMASTER." as SM ,".ADSPSCEMASTER." AM,".CATEGORIES." CA where SM.adsiteId=AM.adsiteId and AM.langId='".$languageId."' and AM.UserId='".$_SESSION['siteuserId']."' and SM.adcategoryId=CA.adcategoryId ";

$sql_details="select * from ".SITEMASTER." as SM ,".ADSPSCEMASTER." AM,".ADSPACEDETAILS." SD,".CATEGORIES." CA where SM.adsiteId=AM.adsiteId and AM.langId='".$languageId."' and AM.UserId='".$_SESSION['siteuserId']."' and AM.adspaceId=SD.adspaceId and SM.adcategoryId=CA.adcategoryId and AM.description <> '' order by AM.readyDate desc ";
//echo $sql_details;die;
}
$query_details=mysql_query($sql_details);
$num=mysql_num_rows($query_details);

 $query_site_name=mysql_query("select adSiteName from adsite_master where siteuserId='".$_SESSION['siteuserId']."'");

?>

<script language="JavaScript">
function show(argu)
{
	//alert(argu);
	document.adspaces.space_type.value=argu;
	document.adspaces.submit();
}
function show_site_nane(argu1)
{
	//alert(argu1);
	document.adspaces.site_name.value=argu1;
	document.adspaces.submit();
}
function change_status(argu3)
{
	//alert(argu3);

	document.adspaces.Id.value=argu3;
	document.adspaces.mode.value="change_status";
	document.adspaces.submit();
}
</script>

<form name="adspaces" action="" method="POST">
<input type="hidden" name="space_type" value="">
<input type="hidden" name="mode" value="">
<input type="hidden" name="site_name" value="">
<input type="hidden" name="Id" value="">

<table width="90%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td valign="top" align="center"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                  <tr>
                    <td valign="top"></td>
                    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td class="PinkTwosideBorder" valign="top"><table width="100%" class="center" cellspacing="0" cellpadding="0">
 
  <tr>
    <td align="left" valign="top">

<!-- start table  -->

<table id="spacesList" style="width:100%;margin-left:1px;position:relative" align="center" border="0" >

<tr>
    <td colspan="2" align="center" class="sky font-18" style="padding-bottom:20px;">
	<?php echo MY_AD_SPACE; ?>
	</td>
<?php
if($GLOBALS['err_msg']!="")
{
?>
<tr>
    <td colspan="2" align="center" class="ERR" style="padding-bottom:10px;">
	<strong><?php echo $GLOBALS['err_msg'];?></strong>
	</td>
</tr>
<?php
}
?>
  <tr>
    <td style="text-align:left;white-space:nowrap;padding-bottom:10px;" colspan="2">

      <span class="font-12 black advertise_tableTxt"><?php echo SITE_NAME; ?>:
        <select id="siteId" name="site_id" class="InputBox" style="width: 150px;" maxlength="150" onchange="return show_site_nane(this.value);">
          <option value="">All</option>
			<?php
			while($row_site_name=mysql_fetch_array($query_site_name))
			{
			?>
			<option   value="<?php echo $row_site_name['adSiteName'];?>"><?php echo $row_site_name['adSiteName'];?></option>
			<?php
			}
			?>
          </select>
      </span>
      <span class="font-12 black advertise_tableTxt">
        <?php echo AD_TYPE; ?>:
        <select id="adspaceType" name="adspace_type" class="InputBox" style="width: 150px;" maxlength="150" onchange="return show(this.value);">
          <option value="">All</option>

          <option value="text" >Text</option>
          <option value="banner">Banner</option>
          <option value="gtb" >Thumbnail</option>
          <option value="cb" >Commercial Break</option>
          </select>

      </span>
	<span class="font-12 black advertise_tableTxt">
	<input type="button" name="add_button" id="add" value="<?php echo SHOW_ALL; ?>" class="blank-button-big-add" onclick="javascript: window.location='all_adspaces.php';" />
	</span>
    </td>
   
  </tr>


  <tr>

    <td colspan="2">

<table width="100%" border="1" cellspacing="0" cellpadding="0">
 <TR>
  <TD>
   <table id="adspaceListTable" style="width:100%" class="login_txt" cellspacing="1" cellpadding="0" border="0">
    <tr>
        <th rowspan="1" class="listHeader" height="30" nowrap="true">
        <strong><?php echo AD_SPACE_NAME; ?></strong>
        </th>
        <th rowspan="1" class="listHeader" nowrap="true">
        <?php echo SITE_NAME; ?>
        </th>
        <th rowspan="1" class="listHeader" nowrap="true">
        <?php echo AD_TYPE; ?>
        </th>
        <th rowspan="1" class="listHeader" nowrap="true">
        # <?php echo OF_ADS; ?>
        </th>
        <th rowspan="1" class="listHeader" nowrap="true">
        <?php echo AVG_CLICKS_PER_DAY; ?>
        </th>
       <th rowspan="1" class="listHeader" nowrap="true">
        <?php echo AVG_CPC; ?>
       </th>
       <th rowspan="1" class="listHeader" nowrap="true">
        <?php echo AVG_IMP; ?>
       </th>
       <th rowspan="1" colspan="1" class="listHeader" nowrap="true"><?php echo AD_DURATION; ?></th>
       <th rowspan="1" colspan="1" class="listHeader" nowrap="true"><?php echo AD_PRICE; ?></th>
       <th rowspan="1" colspan="1" class="listHeader" nowrap="true"><?php echo AD_STATUS; ?></th>
       </tr>

	    <?php
		if($num>0)
		{

		$loop=0;
		while($rec_details=mysql_fetch_array($query_details))
		{
		$loop++;
		if($loop%2 == 0) 
		{
		$class1="normalActive2";
		$class2="normal2";
		$class3="normal2";
		$class4="gray_sitename2";
		}
		else 
		{
		$class1="normalActive";
		$class2="normal";
		$class3="normal";
		$class4="white_sitename2";
		}

		  $sql_price="select * from ".PRICE." where adspaceId='".$rec_details['adspaceId']."' and auto_manu='".$rec_details['priceType']."' ";
		$query_price=mysql_query($sql_price);

		$sqlPrice="select * FROM ".PRICE." where adspaceId='".$rec_details[adspaceId]."' order by duration asc";
		$queryPrice=mysql_query($sqlPrice);

		
	    ?>

        <tr onmouseover="this.className='<?php echo $class1;?>'" onmouseout="this.className='<?php echo $class2;?>'" class="<?php echo $class3;?>">
        <td class="<?php echo $class4;?>">
		<a href="edit_adspace.php?tab=sell_ads&subMenu=4&sid=<?php echo $rec_details['adspaceId'];?>" class="register_link"><?php echo $rec_details['adSpaceName'];?></a>&nbsp;
		</td>
        <td class="<?php echo $class4;?>">
		<a class="register_link" href="edit_websites.php?tab=sell_ads&subMenu=3&sid=<?php echo $rec_details['adsiteId'];?>"><?php echo $rec_details['adSiteName'];?></a>&nbsp;
		</td>

        <td class="<?php echo $class4;?>" align="center">
		<?php echo $rec_details['adspaceType'];?>&nbsp;
		</td>
        <td class="<?php echo $class4;?>" align="center">
		<?php
		// echo $rec_details['adspaceId'];
		$query_no_ads=mysql_fetch_array(mysql_query("select count(*) from advertise_details where adspaceId='".$rec_details['adspaceId']."' "));
		echo $query_no_ads['0'];
		?>&nbsp;
		</td>
        <td class="<?php echo $class4;?>" align="center">
<?php
$resultPrice= mysql_fetch_array($queryPrice);
			if($rec_details[clickPerDay]==0)
			{
				$clickPerDay = '*New';
				$avgCPC = '*New';
			}
			else
			{
				
				$clickPerDay = $rec_details[clickPerDay]*$resultPrice[duration];
				$avgCPC = '$'.number_format($resultPrice[ammount]/$rec_details[clickPerDay], 2, '.', '');
			}
			
		echo $clickPerDay;
		?>&nbsp;
</td>
        <td class="<?php echo $class4;?>" align="center"><?php echo $avgCPC;?>&nbsp;</td>
        <td class="<?php echo $class4;?>" align="center">
		<?php
		if($rec_details[clickPerDay]>0)
		{
			if($rec_details[clickPerDay]>$GLOBALS['avgImpression'])
			{
				//echo $rec=($rec_details[clickPerDay]/$GLOBALS['avgImpression']);
				echo $rec = number_format($rec_details[clickPerDay]/$GLOBALS['avgImpression'], 2, '.', '');
			}
			else
			{
				echo "0";
			}
		}
		else
			{
				echo "0";
			}
		?>&nbsp;
		</td>
        <td class="<?php echo $class4;?>" align="center">
		
		<?php
		$query_price1=mysql_query("select * from ".PRICE." where adspaceId='".$rec_details['adspaceId']."' and auto_manu='".$rec_details['priceType']."' ");
	  $num_deys=mysql_num_rows($query_price1);
		if($num_deys!=0)
		{
		while($rec_deys=mysql_fetch_array($query_price1))
		{
		?>
		<div>
			<?php
			
				echo $rec_deys['duration'];
			
			?>
</div>
		<?php
		}
		}
		else
		{
			echo "0";
		}
		?>
		&nbsp;
		</td>

        <td class="<?php echo $class4;?>" align="center">
		<?php
		 $num_price=mysql_num_rows($query_price);
		if($num_price>0)
		{
			while($rec_price=mysql_fetch_array($query_price))
			{
			?>
			<div><?php echo $rec_price['ammount'];?></div>
			<?php
			}
		}
		else
		{
			echo "0";
		}
		?>&nbsp;
		</td>
		<td class="<?php echo $class4;?>" align="center">
		 <a href="javascript:change_status('<?php echo $rec_details['adspaceId'];?>')" title="Activate Deactivate Status" class="register_link"> 
        <?
	  		$status=mysql_fetch_array(mysql_query("select status from ".ADSPSCEMASTER." where adspaceId='".$rec_details['adspaceId']."' "));
			if($status['status']=='N')
			{				
	 ?>
      			
				<font color="#CC0000"><?php echo AD_BLOCK; ?></font>
      <? 		} 
	  		else
			{ ?>
      			<font color=\"#66CC00\"><?php echo AD_ACTIVE; ?></font>
      <? 
	  		} 
	  ?>
      </a>
		&nbsp;
		</td>
        </tr>
	<?php
	}
	} // end if
	else
	{
	?>
	   <tr><TD colspan="10" align="center" class="<?php echo $class4;?>" height="30"><?php echo NO_AD_RECORD; ?></TD></tr>

	<?php
	}
	?>

       </table>

    </td>
  </tr>
</table>

   </td>
  </tr>
</table>

<!-- e table  -->

</td>
</tr>
</table></td>
                      </tr>
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                    </table></td>
                    <td>&nbsp;</td>
                    </tr>
                </table></td>
              </tr>
            </table>
</form>
            
<?php
        //include("footer.php");
}
		?> 